﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Medical.Business;
using Medical.Entity;


/// <summary>
/// 管理员操作
/// </summary>
public partial class webadmin_DoAdmin : AdminPage
{
    /// <summary>
    /// ID
    /// </summary>
    protected int nID;

    /// <summary>
    /// 操作类型
    /// </summary>
    protected string strType = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        strType = Common.GetQueryValue("type", 0);
        int.TryParse(Common.GetQueryValue("id", 0), out nID);
        if (!Page.IsPostBack)
        {
            InitType();
            if (strType == "edit")
            {
                //页面数据初始化
                InitData();
            }
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return 1;
    }

    protected void InitType()
    {
        DataTable dt = new BisAdminType().GetList();
        ddlType.Items.Add(new ListItem("请选择", "0"));
        if (dt != null && dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["Features"].ToString() != "1")
                    ddlType.Items.Add(new ListItem(dr["Name"].ToString(), dr["ID"].ToString()));
            }
        }
    }

    protected void InitData()
    {
        AdminInfo Info = new BisAdminInfo().Get(nID);
        if (Info != null)
        {
            txtUserName.Text = Info.UserName;
            txtUserName.Enabled = false;
            txtName.Text = Info.Name;
            ddlType.SelectedValue = Info.AdminTypeID.ToString();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string strUserName=txtUserName.Text.Trim();
        BisAdminInfo bisInfo = new BisAdminInfo();
        DataTable dtInfo = bisInfo.GetAdminInfo(strUserName);
        if (strType != "edit"&&dtInfo != null && dtInfo.Rows.Count>0)
        {
            Until.Alert("用户名已经存在!");
            return;
        }
        AdminInfo Info = new AdminInfo();
        Info.UserName = strUserName;
        if (!string.IsNullOrEmpty(txtPasswords.Text.Trim()))
        {
            Info.Passwords = Common.md5(txtPasswords.Text.Trim());
        }
        Info.Name = txtName.Text.Trim();
        Info.AdminTypeID = Convert.ToInt32(ddlType.SelectedValue);
        bool bResult = false;
        if (strType == "edit")
        {
            Info.ID = nID;
            bResult = bisInfo.UpdateAdmin(Info);
        }
        else
        {
            bResult = bisInfo.Insert(Info);
        }
        //结果处理
        if (bResult)
        {
            Until.AlertJS("提交成功！", "ResetPage();");
        }
        else
        {
            Until.Alert("提交失败！");
        }        
    }
}